﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace console.demo.senior
{
    public class IntroToLING
    {

        public IntroToLING() { }

        public static void Test()
        {
            // 创建数据源
            int[] numbers = { 1, 2, 3, 4, 5, 6, 7 };
            //创建查询
            var numQuery = numbers.Select(c => c % 2 == 0).ToArray();
            //执行查询
            foreach (var num in numQuery)
            {
                Console.WriteLine("{0,1}", num);
            }
        }
        public static void get()
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer("xxx", "hfusd", "cs"));
            List<Distributor> distributors = new List<Distributor>();
            distributors.Add(new Distributor("xxx", "sdfjis", 10));
            /**
             * LINQ 查询变量被类型化为 IEnumerable<T> 或派生类型（如 IQueryable<T>）。 看到类型化为 IEnumerable<Customer> 的查询变量时，这只意味着执行查询时，该查询将生成包含零个或多个 Customer 对象的序列。
             */
            var list = (from customer in customers where customer.City != "London" select customer).ToList();
            // 搜索结果并排序
            var queryLondonCustomers = from cust in customers where cust.City == "london" orderby cust.Name ascending select cust;
            // 分组查询
            var queryCustomerByCity = from cust in customers group cust by cust.City;
            var innerJoinQuery = from cust in customers join dist in distributors on cust.City equals dist.City select new { CustomerName = cust.Name, DistributeName = dist.Name };
            Console.WriteLine(innerJoinQuery.ToArray());
            // 连接数据
            // Create the first data source.
            List<Student> students = new List<Student>()
        {
            new Student { First="Svetlana",
                Last="Omelchenko",
                ID=111,
                Street="123 Main Street",
                City="Seattle",
                Scores= new List<int> { 97, 92, 81, 60 } },
            new Student { First="Claire",
                Last="O’Donnell",
                ID=112,
                Street="124 Main Street",
                City="Redmond",
                Scores= new List<int> { 75, 84, 91, 39 } },
            new Student { First="Sven",
                Last="Mortensen",
                ID=113,
                Street="125 Main Street",
                City="Lake City",
                Scores= new List<int> { 88, 93, 65, 91 } },
        };

            // Create the second data source.
            List<Teacher> teachers = new List<Teacher>()
        {
            new Teacher { First="Ann", Last="Beebe", ID=945, City="Seattle" },
            new Teacher { First="Alex", Last="Robinson", ID=956, City="Redmond" },
            new Teacher { First="Michiyo", Last="Sato", ID=972, City="Tacoma" }
        };
            var perpleInSeattle = (from student in students where student.City == "Seattle" select student.Last).Concat(from teacher in teachers where teacher.City == "Seattle" select teacher.Last);
            Console.WriteLine(perpleInSeattle.ToArray().Length);
            var query = from cust in customers select new { Name = cust.Name, City = cust.City };
            Console.WriteLine(query.Count());

            foreach (var customer in list)
            {
                Console.WriteLine(customer.Name);
            }
            foreach (var customerGroup in queryCustomerByCity)
            {
                Console.WriteLine(customerGroup.Key);
                foreach (Customer customer in customerGroup)
                {
                    Console.WriteLine(customer.Name);
                }
            }
        }
        public static void testMethod()
        {
            int[] numbers = { 5, 10, 8, 4, 6, 12 };
            IEnumerable<int> numQuery1 = from num in numbers where num % 2 == 0 select num;
            // 方法写法
            IEnumerable<int> numQuery2 = numbers.Where(num => num % 2 == 0).OrderBy(n => n);
            foreach (var i in numQuery2)
            {
                Console.WriteLine(i);
            }
            Console.ReadKey();
        }
    }
}
